package hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts;

import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.policies.DesktopItemSemanticEditPolicy;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.part.AwesomeVisualIDRegistry;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.providers.AwesomeElementTypes;

import java.util.Collections;
import java.util.List;

import org.eclipse.draw2d.BorderLayout;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.RoundedRectangle;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.LayoutEditPolicy;
import org.eclipse.gef.editpolicies.NonResizableEditPolicy;
import org.eclipse.gef.handles.MoveHandle;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gmf.runtime.diagram.core.edithelpers.CreateElementRequestAdapter;
import org.eclipse.gmf.runtime.diagram.ui.editparts.AbstractBorderedShapeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IBorderItemEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.BorderItemSelectionEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
import org.eclipse.gmf.runtime.diagram.ui.figures.BorderItemLocator;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewAndElementRequest;
import org.eclipse.gmf.runtime.draw2d.ui.figures.ConstrainedToolbarLayout;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure;
import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.gmf.tooling.runtime.edit.policies.reparent.CreationEditPolicyWithCustomReparent;
import org.eclipse.swt.graphics.Color;

/**
 * @generated
 */
public class DesktopEditPart extends AbstractBorderedShapeEditPart {

	/**
	 * @generated
	 */
	public static final int VISUAL_ID = 2001;

	/**
	 * @generated
	 */
	protected IFigure contentPane;

	/**
	 * @generated
	 */
	protected IFigure primaryShape;

	/**
	 * @generated
	 */
	public DesktopEditPart(View view) {
		super(view);
	}

	/**
	 * @generated
	 */
	protected void createDefaultEditPolicies() {
		installEditPolicy(EditPolicyRoles.CREATION_ROLE,
				new CreationEditPolicyWithCustomReparent(
						AwesomeVisualIDRegistry.TYPED_INSTANCE));
		super.createDefaultEditPolicies();
		installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE,
				new DesktopItemSemanticEditPolicy());
		installEditPolicy(EditPolicy.LAYOUT_ROLE, createLayoutEditPolicy());
		// XXX need an SCR to runtime to have another abstract superclass that would let children add reasonable editpolicies
		// removeEditPolicy(org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles.CONNECTION_HANDLES_ROLE);
	}

	/**
	 * @generated
	 */
	protected LayoutEditPolicy createLayoutEditPolicy() {
		org.eclipse.gmf.runtime.diagram.ui.editpolicies.LayoutEditPolicy lep = new org.eclipse.gmf.runtime.diagram.ui.editpolicies.LayoutEditPolicy() {

			protected EditPolicy createChildEditPolicy(EditPart child) {
				View childView = (View) child.getModel();
				switch (AwesomeVisualIDRegistry.getVisualID(childView)) {
				case Desktop2EditPart.VISUAL_ID:
					return new BorderItemSelectionEditPolicy() {

						protected List createSelectionHandles() {
							MoveHandle mh = new MoveHandle(
									(GraphicalEditPart) getHost());
							mh.setBorder(null);
							return Collections.singletonList(mh);
						}
					};
				}
				EditPolicy result = child
						.getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE);
				if (result == null) {
					result = new NonResizableEditPolicy();
				}
				return result;
			}

			protected Command getMoveChildrenCommand(Request request) {
				return null;
			}

			protected Command getCreateCommand(CreateRequest request) {
				return null;
			}
		};
		return lep;
	}

	/**
	 * @generated
	 */
	protected IFigure createNodeShape() {
		return primaryShape = new DesktopFigure();
	}

	/**
	 * @generated
	 */
	public DesktopFigure getPrimaryShape() {
		return (DesktopFigure) primaryShape;
	}

	/**
	 * @generated
	 */
	protected boolean addFixedChild(EditPart childEditPart) {
		if (childEditPart instanceof DesktopMenusCompartmentEditPart) {
			IFigure pane = getPrimaryShape().getFigureDesktopCenter();
			setupContentPane(pane); // FIXME each comparment should handle his content pane in his own way 
			pane.add(((DesktopMenusCompartmentEditPart) childEditPart)
					.getFigure());
			return true;
		}
		if (childEditPart instanceof DesktopTopBoxCompartmentEditPart) {
			IFigure pane = getPrimaryShape().getFigureDesktopTopRectangle();
			setupContentPane(pane); // FIXME each comparment should handle his content pane in his own way 
			pane.add(((DesktopTopBoxCompartmentEditPart) childEditPart)
					.getFigure());
			return true;
		}
		if (childEditPart instanceof DesktopBottomBoxCompartmentEditPart) {
			IFigure pane = getPrimaryShape().getFigureDesktopBottomRectangle();
			setupContentPane(pane); // FIXME each comparment should handle his content pane in his own way 
			pane.add(((DesktopBottomBoxCompartmentEditPart) childEditPart)
					.getFigure());
			return true;
		}
		if (childEditPart instanceof DesktopRightBoxCompartmentEditPart) {
			IFigure pane = getPrimaryShape().getFigureDesktopRightRectangle();
			setupContentPane(pane); // FIXME each comparment should handle his content pane in his own way 
			pane.add(((DesktopRightBoxCompartmentEditPart) childEditPart)
					.getFigure());
			return true;
		}
		if (childEditPart instanceof DesktopLeftBoxCompartmentEditPart) {
			IFigure pane = getPrimaryShape().getFigureDesktopLeftRectangle();
			setupContentPane(pane); // FIXME each comparment should handle his content pane in his own way 
			pane.add(((DesktopLeftBoxCompartmentEditPart) childEditPart)
					.getFigure());
			return true;
		}
		return false;
	}

	/**
	 * @generated
	 */
	protected boolean removeFixedChild(EditPart childEditPart) {
		if (childEditPart instanceof DesktopMenusCompartmentEditPart) {
			IFigure pane = getPrimaryShape().getFigureDesktopCenter();
			pane.remove(((DesktopMenusCompartmentEditPart) childEditPart)
					.getFigure());
			return true;
		}
		if (childEditPart instanceof DesktopTopBoxCompartmentEditPart) {
			IFigure pane = getPrimaryShape().getFigureDesktopTopRectangle();
			pane.remove(((DesktopTopBoxCompartmentEditPart) childEditPart)
					.getFigure());
			return true;
		}
		if (childEditPart instanceof DesktopBottomBoxCompartmentEditPart) {
			IFigure pane = getPrimaryShape().getFigureDesktopBottomRectangle();
			pane.remove(((DesktopBottomBoxCompartmentEditPart) childEditPart)
					.getFigure());
			return true;
		}
		if (childEditPart instanceof DesktopRightBoxCompartmentEditPart) {
			IFigure pane = getPrimaryShape().getFigureDesktopRightRectangle();
			pane.remove(((DesktopRightBoxCompartmentEditPart) childEditPart)
					.getFigure());
			return true;
		}
		if (childEditPart instanceof DesktopLeftBoxCompartmentEditPart) {
			IFigure pane = getPrimaryShape().getFigureDesktopLeftRectangle();
			pane.remove(((DesktopLeftBoxCompartmentEditPart) childEditPart)
					.getFigure());
			return true;
		}
		return false;
	}

	/**
	 * @generated
	 */
	protected void addChildVisual(EditPart childEditPart, int index) {
		if (addFixedChild(childEditPart)) {
			return;
		}
		super.addChildVisual(childEditPart, -1);
	}

	/**
	 * @generated
	 */
	protected void removeChildVisual(EditPart childEditPart) {
		if (removeFixedChild(childEditPart)) {
			return;
		}
		super.removeChildVisual(childEditPart);
	}

	/**
	 * @generated
	 */
	protected IFigure getContentPaneFor(IGraphicalEditPart editPart) {
		if (editPart instanceof DesktopMenusCompartmentEditPart) {
			return getPrimaryShape().getFigureDesktopCenter();
		}
		if (editPart instanceof DesktopTopBoxCompartmentEditPart) {
			return getPrimaryShape().getFigureDesktopTopRectangle();
		}
		if (editPart instanceof DesktopBottomBoxCompartmentEditPart) {
			return getPrimaryShape().getFigureDesktopBottomRectangle();
		}
		if (editPart instanceof DesktopRightBoxCompartmentEditPart) {
			return getPrimaryShape().getFigureDesktopRightRectangle();
		}
		if (editPart instanceof DesktopLeftBoxCompartmentEditPart) {
			return getPrimaryShape().getFigureDesktopLeftRectangle();
		}
		if (editPart instanceof IBorderItemEditPart) {
			return getBorderedFigure().getBorderItemContainer();
		}
		return getContentPane();
	}

	/**
	 * @generated
	 */
	protected void addBorderItem(IFigure borderItemContainer,
			IBorderItemEditPart borderItemEditPart) {
		if (borderItemEditPart instanceof Desktop2EditPart) {
			BorderItemLocator locator = new BorderItemLocator(getMainFigure(),
					PositionConstants.SOUTH);
			locator.setBorderItemOffset(new Dimension(-20, -20));
			borderItemContainer.add(borderItemEditPart.getFigure(), locator);
		} else {
			super.addBorderItem(borderItemContainer, borderItemEditPart);
		}
	}

	/**
	 * @generated
	 */
	protected NodeFigure createNodePlate() {
		DefaultSizeNodeFigure result = new DefaultSizeNodeFigure(40, 40);
		return result;
	}

	/**
	 * Creates figure for this edit part.
	 * 
	 * Body of this method does not depend on settings in generation model
	 * so you may safely remove <i>generated</i> tag and modify it.
	 * 
	 * @generated
	 */
	protected NodeFigure createMainFigure() {
		NodeFigure figure = createNodePlate();
		figure.setLayoutManager(new StackLayout());
		IFigure shape = createNodeShape();
		figure.add(shape);
		contentPane = setupContentPane(shape);
		return figure;
	}

	/**
	 * Default implementation treats passed figure as content pane.
	 * Respects layout one may have set for generated figure.
	 * @param nodeShape instance of generated figure class
	 * @generated
	 */
	protected IFigure setupContentPane(IFigure nodeShape) {
		if (nodeShape.getLayoutManager() == null) {
			ConstrainedToolbarLayout layout = new ConstrainedToolbarLayout();
			layout.setSpacing(5);
			nodeShape.setLayoutManager(layout);
		}
		return nodeShape; // use nodeShape itself as contentPane
	}

	/**
	 * @generated
	 */
	public IFigure getContentPane() {
		if (contentPane != null) {
			return contentPane;
		}
		return super.getContentPane();
	}

	/**
	 * @generated
	 */
	protected void setForegroundColor(Color color) {
		if (primaryShape != null) {
			primaryShape.setForegroundColor(color);
		}
	}

	/**
	 * @generated
	 */
	protected void setBackgroundColor(Color color) {
		if (primaryShape != null) {
			primaryShape.setBackgroundColor(color);
		}
	}

	/**
	 * @generated
	 */
	protected void setLineWidth(int width) {
		if (primaryShape instanceof Shape) {
			((Shape) primaryShape).setLineWidth(width);
		}
	}

	/**
	 * @generated
	 */
	protected void setLineType(int style) {
		if (primaryShape instanceof Shape) {
			((Shape) primaryShape).setLineStyle(style);
		}
	}

	/**
	 * @generated
	 */
	public EditPart getPrimaryChildEditPart() {
		return getChildBySemanticHint(AwesomeVisualIDRegistry
				.getType(Desktop2EditPart.VISUAL_ID));
	}

	/**
	 * @generated
	 */
	public EditPart getTargetEditPart(Request request) {
		if (request instanceof CreateViewAndElementRequest) {
			CreateElementRequestAdapter adapter = ((CreateViewAndElementRequest) request)
					.getViewAndElementDescriptor()
					.getCreateElementRequestAdapter();
			IElementType type = (IElementType) adapter
					.getAdapter(IElementType.class);
			if (type == AwesomeElementTypes.TopBox_3003) {
				return getChildBySemanticHint(AwesomeVisualIDRegistry
						.getType(DesktopTopBoxCompartmentEditPart.VISUAL_ID));
			}
			if (type == AwesomeElementTypes.BottomBox_3004) {
				return getChildBySemanticHint(AwesomeVisualIDRegistry
						.getType(DesktopBottomBoxCompartmentEditPart.VISUAL_ID));
			}
			if (type == AwesomeElementTypes.RightBox_3006) {
				return getChildBySemanticHint(AwesomeVisualIDRegistry
						.getType(DesktopRightBoxCompartmentEditPart.VISUAL_ID));
			}
			if (type == AwesomeElementTypes.LeftBox_3005) {
				return getChildBySemanticHint(AwesomeVisualIDRegistry
						.getType(DesktopLeftBoxCompartmentEditPart.VISUAL_ID));
			}
		}
		return super.getTargetEditPart(request);
	}

	/**
	 * @generated
	 */
	public class DesktopFigure extends RoundedRectangle {

		/**
		 * @generated
		 */
		private RectangleFigure fFigureDesktopCenter;
		/**
		 * @generated
		 */
		private RectangleFigure fFigureDesktopTopRectangle;
		/**
		 * @generated
		 */
		private RectangleFigure fFigureDesktopBottomRectangle;
		/**
		 * @generated
		 */
		private RectangleFigure fFigureDesktopLeftRectangle;
		/**
		 * @generated
		 */
		private RectangleFigure fFigureDesktopRightRectangle;

		/**
		 * @generated
		 */
		public DesktopFigure() {

			BorderLayout layoutThis = new BorderLayout();
			this.setLayoutManager(layoutThis);

			this.setCornerDimensions(new Dimension(getMapMode().DPtoLP(8),
					getMapMode().DPtoLP(8)));
			createContents();
		}

		/**
		 * @generated
		 */
		private void createContents() {

			WrappingLabel desktopTitleLabel0 = new WrappingLabel();

			desktopTitleLabel0.setText("Desktop");

			this.add(desktopTitleLabel0, BorderLayout.TOP);

			RectangleFigure desktopBody0 = new RectangleFigure();

			this.add(desktopBody0, BorderLayout.CENTER);

			BorderLayout layoutDesktopBody0 = new BorderLayout();
			desktopBody0.setLayoutManager(layoutDesktopBody0);

			fFigureDesktopTopRectangle = new RectangleFigure();

			fFigureDesktopTopRectangle.setPreferredSize(new Dimension(
					getMapMode().DPtoLP(560), getMapMode().DPtoLP(20)));

			desktopBody0.add(fFigureDesktopTopRectangle, BorderLayout.TOP);

			BorderLayout layoutFFigureDesktopTopRectangle = new BorderLayout();
			fFigureDesktopTopRectangle
					.setLayoutManager(layoutFFigureDesktopTopRectangle);

			fFigureDesktopBottomRectangle = new RectangleFigure();

			fFigureDesktopBottomRectangle.setPreferredSize(new Dimension(
					getMapMode().DPtoLP(560), getMapMode().DPtoLP(20)));

			desktopBody0
					.add(fFigureDesktopBottomRectangle, BorderLayout.BOTTOM);

			BorderLayout layoutFFigureDesktopBottomRectangle = new BorderLayout();
			fFigureDesktopBottomRectangle
					.setLayoutManager(layoutFFigureDesktopBottomRectangle);

			fFigureDesktopLeftRectangle = new RectangleFigure();

			fFigureDesktopLeftRectangle.setPreferredSize(new Dimension(
					getMapMode().DPtoLP(20), getMapMode().DPtoLP(320)));

			desktopBody0.add(fFigureDesktopLeftRectangle, BorderLayout.LEFT);

			BorderLayout layoutFFigureDesktopLeftRectangle = new BorderLayout();
			fFigureDesktopLeftRectangle
					.setLayoutManager(layoutFFigureDesktopLeftRectangle);

			fFigureDesktopRightRectangle = new RectangleFigure();

			fFigureDesktopRightRectangle.setPreferredSize(new Dimension(
					getMapMode().DPtoLP(20), getMapMode().DPtoLP(320)));

			desktopBody0.add(fFigureDesktopRightRectangle, BorderLayout.RIGHT);

			BorderLayout layoutFFigureDesktopRightRectangle = new BorderLayout();
			fFigureDesktopRightRectangle
					.setLayoutManager(layoutFFigureDesktopRightRectangle);

			fFigureDesktopCenter = new RectangleFigure();

			fFigureDesktopCenter.setBackgroundColor(ColorConstants.darkGreen);
			fFigureDesktopCenter.setPreferredSize(new Dimension(getMapMode()
					.DPtoLP(520), getMapMode().DPtoLP(320)));
			fFigureDesktopCenter.setMinimumSize(new Dimension(getMapMode()
					.DPtoLP(300), getMapMode().DPtoLP(160)));

			desktopBody0.add(fFigureDesktopCenter, BorderLayout.CENTER);

		}

		/**
		 * @generated
		 */
		public RectangleFigure getFigureDesktopCenter() {
			return fFigureDesktopCenter;
		}

		/**
		 * @generated
		 */
		public RectangleFigure getFigureDesktopTopRectangle() {
			return fFigureDesktopTopRectangle;
		}

		/**
		 * @generated
		 */
		public RectangleFigure getFigureDesktopBottomRectangle() {
			return fFigureDesktopBottomRectangle;
		}

		/**
		 * @generated
		 */
		public RectangleFigure getFigureDesktopLeftRectangle() {
			return fFigureDesktopLeftRectangle;
		}

		/**
		 * @generated
		 */
		public RectangleFigure getFigureDesktopRightRectangle() {
			return fFigureDesktopRightRectangle;
		}

	}

}
